﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.Base;
using ZBJF.Warranty.Common.Enums.InsuranceWarranty;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.InsuranceWarranty;
using ZBJF.Warranty.Model.ViewModel.Product;

namespace ZBJF.Warranty.WebProvider
{
    public class ConvertToSelector
    {
        private ConvertToSelector()
        {
        }

        private static ConvertToSelector _instance;
        private static readonly object Lock = new object();
        public static ConvertToSelector Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ConvertToSelector();
                    lock (Lock)
                    {
                        if (_instance == null)
                        {
                            _instance = new ConvertToSelector();
                        }
                    }
                }
                return _instance;
            }
        }
        public SelectList InitSelectList(bool hasInit = false)
        {
            List<SelectListItem> items = GetEmptySelect(hasInit);

            items.AddRange(new List<SelectListItem>()
            {
                new SelectListItem() {Text ="是", Value = bool.TrueString},
                new SelectListItem() {Text = "否", Value = bool.FalseString}
            });
            return new SelectList(items, "Value", "Text");
        }

        public SelectList EnumTypeConvertToSelectList(Type enumType, int selectedItemValue = 0, bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);

            var list = EnumHelper.Instance.GetSortedList(enumType);
            foreach (DictionaryEntry item in list)
            {
                items.AddRange(
                    new List<SelectListItem>
                        {
                            new SelectListItem
                            {
                                Text = item.Value.ToString(),
                                Value = item.Key.ToString()
                            },
                        });
            }

            SelectList generateList = new SelectList(items, "Value", "Text", selectedItemValue);
            return generateList;
        }

        public SelectList InsuranceWarrantyIsProcessingConvertToSelectList(Type enumType, int selectedItemValue = 0, bool addEmptySelect = true, string isProcessing = "")
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);

            var list = EnumHelper.Instance.GetSortedList(enumType);
            foreach (DictionaryEntry item in list)
            {
                items.AddRange(
                        new List<SelectListItem>
                    {
                                new SelectListItem
                                {
                                    Text = item.Value.ToString(),
                                    Value = item.Key.ToString(),
                                    Selected = (item.Key.ToString() == selectedItemValue.ToString())
                                },
                    });
            }
            List<string> a = new List<string>();
            if (isProcessing == "false")
            {
                a = new List<string>(new string[]
                {  "",
                    ((byte)InsuranceWarrantyStatus.Close).ToString(),
                    //((byte)InsuranceWarrantyStatus.Refuse).ToString(),
                    //((byte)InsuranceWarrantyStatus.Retreat).ToString(),
                    ((byte)InsuranceWarrantyStatus.RetrurnVisited).ToString()
                });
            }
            else if (isProcessing == "true")
            {
                a = new List<string>(new string[]
             {
                 "",
                //((byte)InsuranceWarrantyStatus.HaveContact).ToString(),
                //((byte)InsuranceWarrantyStatus.NeedReContact).ToString(),
                ((byte)InsuranceWarrantyStatus.Created).ToString(), 
                //((byte)InsuranceWarrantyStatus.Verified).ToString(),
                //((byte)InsuranceWarrantyStatus.HaveArrangeSendToFix).ToString(),
                //((byte)InsuranceWarrantyStatus.HaveArrangeToStore).ToString(), 
                //((byte)InsuranceWarrantyStatus.HaveArrangeToVisit).ToString(),
                //((byte)InsuranceWarrantyStatus.HavePartition).ToString(),
                //((byte)InsuranceWarrantyStatus.CollectedFixVouchers).ToString(),
                //((byte)InsuranceWarrantyStatus.AndRevised).ToString(),
                //((byte)InsuranceWarrantyStatus.NoMaintenance).ToString()
             });
            }
            if (isProcessing != "")
            {
                items = items.Where(t => a.Contains(t.Value)).ToList();
            }
            SelectList generateList = new SelectList(items, "Value", "Text", selectedItemValue);
            return generateList;
        }
        //if (insuranceWarrantyRecordTaskType > 0)
        //{
        //}

        public SelectList ProductTypeConvertToSelectList(bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);

            items.AddRange(ServiceFactory.ProductTypeServiceInstance.GetProductType().ToList().Select(o => new SelectListItem
            {
                Text = o.ProductName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }

        public SelectList ProductBrandConvertToSelectList(bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);

            items.AddRange(ServiceFactory.ProductBrandsServiceInstance.GetAllProductBrand().ToList().Select(o => new SelectListItem
            {
                Text = o.BrandName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }
        public SelectList StatusReasonConvertToSelectList(bool addEmptySelect = true, long status = 0)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);

            var list = ServiceFactory.StatusReasonServiceInstance.LoadEntities(n => n.IsAvailable).ToList();
            list = status > 0 ? list.Where(n => n.InsuranceWarrantyRecordStatus.StatusCode == status).ToList() : list;
            items.AddRange(list.Select(o => new SelectListItem
            {
                Text = o.ReasonDescribe,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }
        public SelectList ProductModelsConvertToSelectList(bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);

            items.AddRange(ServiceFactory.ProductModelsServiceInstance.GetProductModel().ToList().Select(o => new SelectListItem
            {
                Text = o.ModelName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }

        /// <summary>
        /// 型号
        /// </summary>
        /// <param name="brandId">品牌id，没有就传0</param>
        /// <param name="addEmptySelect"></param>
        /// <returns></returns>
        public SelectList ProductModelsConvertToSelectList(long brandId, bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            if (brandId < 1)
                return new SelectList(items, "Value", "Text");
            items.AddRange(ServiceFactory.ProductModelsServiceInstance.GetProductModel(brandId).ToList().Select(o => new SelectListItem
            {
                Text = o.ModelName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }

       
        public SelectList AccessoriesLevelConvertToSelectList(bool addEmptySelect = true, int selectedValue = -1)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);

            items.AddRange(ServiceFactory.AccessoriesLevelServiceInstance.LoadEntities(t => t.IsAvailable == true).ToList().Select(o => new SelectListItem
            {
                Text = o.AccessoriesLevelName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text", selectedValue);
        }
       
        public SelectList AccessoriesTypeConvertToSelectList(long productTypeId, bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            if (productTypeId > 0)
            {
                items.AddRange(ServiceFactory.AccessoriesTypeServiceInstance.LoadEntities(t => t.ProductTypeID == productTypeId && t.IsAvailable == true).ToList().Select(o => new SelectListItem
                {
                    Text = o.AccessoriesName,
                    Value = o.ID.ToString()
                }));
            }
            return new SelectList(items, "Value", "Text");
        }


        public SelectList ProvinceConvertToSelectList(bool addEmptySelect = true, int selectedValue = -1)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);

            items.AddRange(ServiceFactory.D_ProvinceServiceInstance.GetDProvinceAll().ToList().Select(o => new SelectListItem
            {
                Text = o.ProvinceName,
                Value = o.ProvinceID.ToString()
            }));
            return new SelectList(items, "Value", "Text", selectedValue);
        }
        public SelectList SellersTypeToSelectList(bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);

            items.AddRange(ServiceFactory.SellersTypeServiceInstance.GetSellersTypeAll().ToList().Select(o => new SelectListItem
            {
                Text = o.SellerTypeName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }
        public SelectList SellerNameToSelectList(bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);

            items.AddRange(ServiceFactory.SellersServiceInstance.GetSellersAll().ToList().Select(o => new SelectListItem
            {
                Text = o.SellersName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }
        public SelectList PareSellersOrganizationToSelectList(bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            items.AddRange(ServiceFactory.SellersOrganizationServiceInstance.GetSellersOrganizationAll().Where(n => n.ParentOrganizationId.Equals(0)).ToList().Select(o => new SelectListItem
            {
                Text = o.OrganizationName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }
        public SelectList SellersOrganizationToSelectList(bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            items.AddRange(ServiceFactory.SellersOrganizationServiceInstance.GetSellersOrganizationAll().Where(n => n.ParentOrganizationId > 0).ToList().Select(o => new SelectListItem
            {
                Text = o.OrganizationName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }
        public SelectList PareSellersOrganizationCompanyToSelectList(bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            items.AddRange(ServiceFactory.SellersOrganizationServiceInstance.GetSellersOrganizationAll().Where(n => n.ParentOrganizationId.Equals(0) && n.IsInsuranceCompany).ToList().Select(o => new SelectListItem
            {
                Text = o.OrganizationName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }
        public SelectList SellersOrganizationCompanyToSelectList(bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            items.AddRange(ServiceFactory.SellersOrganizationServiceInstance.GetSellersOrganizationAll().Where(n => n.ParentOrganizationId > 0 && n.IsInsuranceCompany).ToList().Select(o => new SelectListItem
            {
                Text = o.OrganizationName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }
        //public SelectList InsuranceProductsToSelectList(bool addEmptySelect = true)
        //{
        //    List<SelectListItem> items = GetEmptySelect(addEmptySelect);
        //    var order = ServiceFactory.InsuranceProductsServiceInstance.GetInsuranceProductsAll().Where(n => n.InsuranceProductCategory == 1);
        //    items.AddRange(order.ToList().Select(o => new SelectListItem
        //    {
        //        Text = o.ProductName,
        //        Value = o.ID.ToString()
        //    }));
        //    return new SelectList(items, "Value", "Text");
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="addEmptySelect"></param>
        /// <param name="belongApplicationType">所属系统 </param>
        /// <param name="excludeUserID">不包含此ID的用户</param>
        /// <returns></returns>
        public SelectList SystemUserToSelectList(bool addEmptySelect = true, byte belongApplicationType = 2, long excludeUserID = 0)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            items.AddRange(ServiceFactory.SystemUserServiceInstance.GetSystemUserAll().Where(n => n.BelongApplicationType == belongApplicationType && n.ID != excludeUserID).ToList().Select(o => new SelectListItem
            {
                Text = o.UserDisPlayName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }
        /// <summary>
        /// 信息收集项目类别
        /// </summary>
        /// <param name="addEmptySelect"></param>
        /// <returns></returns>
        public SelectList InfoCollectItemCategoryToSelectList(bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            items.AddRange(ServiceFactory.InsuranceProductsInfoCollectCategoryServiceInstance.LoadByAll().ToList().Select(o => new SelectListItem
            {
                Text = o.InfoCollectCategoryName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }
        private List<SelectListItem> GetEmptySelect(bool addEmptySelect = true)
        {
            List<SelectListItem> items = new List<SelectListItem>();
            if (addEmptySelect)
            {
                items.Add(new SelectListItem()
                {
                    Value = "",
                    Text = "请选择"
                });
            }
            return items;
        }
        /// <summary>
        /// 服务商
        /// </summary>
        /// <param name="addEmptySelect"></param>
        /// <returns></returns>
        public SelectList ServiceProviderConvertToSelectList(bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);

            items.AddRange(ServiceFactory.ServiceProviderServiceInstance.LoadEntities(t => t.IsAvailable == true).ToList().Select(o => new SelectListItem
            {
                Text = o.ServiceProviderName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }
        /// <summary>
        /// 服务商网点
        /// </summary>
        /// <param name="serviceProviderId">服务商ID</param>
        /// <param name="warrantyMaintenanceTypeID">网点服务类型</param>
        /// <returns></returns>
        public SelectList ServiceProviderBranchConvertToSelectList(int? serviceProviderId, byte? warrantyMaintenanceTypeID, ServiceProductType serviceProductType, bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            if (!serviceProviderId.HasValue || serviceProviderId.Value < 1 || !warrantyMaintenanceTypeID.HasValue || warrantyMaintenanceTypeID < 1)
                return new SelectList(items, "Value", "Text");

            items.AddRange(ServiceFactory.ServiceProviderBranchServiceInstance.GetServiceProviderBranch(serviceProviderId.Value, warrantyMaintenanceTypeID.Value,serviceProductType).Select(o => new SelectListItem
            {
                Text = o.BranchName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }
        /// <summary>
        /// 服务商网点
        /// </summary>
        /// <param name="serviceProviderId">服务商ID</param>
        /// <param name="warrantyMaintenanceTypeID">网点服务类型</param>
        /// <param name="addEmptySelect"></param>
        /// <returns></returns>
        public SelectList ServiceProviderBranchConvertToSelectList(int? serviceProviderId, byte? warrantyMaintenanceTypeID,  bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            if (!serviceProviderId.HasValue || serviceProviderId.Value < 1 || !warrantyMaintenanceTypeID.HasValue || warrantyMaintenanceTypeID < 1)
                return new SelectList(items, "Value", "Text");

            items.AddRange(ServiceFactory.ServiceProviderBranchServiceInstance.GetServiceProviderBranch(serviceProviderId.Value, warrantyMaintenanceTypeID.Value).Select(o => new SelectListItem
            {
                Text = o.BranchName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }

        /// <summary>
        /// 服务项目
        /// </summary>
        /// <param name="addEmptySelect"></param>
        /// <returns></returns>
        public SelectList ServiceProjectConvertToSelectList(byte serviceProductType, bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            if (serviceProductType < 1)
                return new SelectList(items, "Value", "Text");
            items.AddRange(ServiceFactory.ServiceProjectServiceInstance.LoadEntities(t => t.IsAvailable == true && t.ServiceProductType == serviceProductType).ToList()
                .Select(o => new SelectListItem
                {
                    Text = o.ProjectName,
                    Value = o.ID.ToString()
                }));
            return new SelectList(items, "Value", "Text");
        }

        /// <summary>
        /// 所在省
        /// </summary>
        /// <param name="addEmptySelect"></param>
        /// <returns></returns>
        public SelectList D_ProvinceConvertToSelectList(bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);

            items.AddRange(ServiceFactory.D_ProvinceServiceInstance.GetDProvinceAll().ToList().Select(o => new SelectListItem
            {
                Text = o.ProvinceName,
                Value = o.ProvinceID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }

        /// <summary>
        /// 所在城市
        /// </summary>
        /// <param name="provinceID"></param>
        /// <param name="addEmptySelect"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        public SelectList D_CityConvertToSelectList(int provinceID, bool addEmptySelect = true, int selectedValue = -1)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            items.AddRange(ServiceFactory.D_CityServiceInstance.LoadEntities(t => t.ProvinceID == provinceID).ToList().Select(o => new SelectListItem
            {
                Text = o.CityName,
                Value = o.CityID.ToString()
            }));
            return new SelectList(items, "Value", "Text", selectedValue);
        }

        /// <summary>
        /// 承保公司
        /// </summary>
        /// <param name="isParent">选择只能是父节点的保险公司</param>
        /// <param name="insuranceCompanyId"></param>
        /// <param name="addEmptySelect"></param>
        /// <returns></returns>
        public SelectList InsuranceCompanyConvertToSelectList(bool isParent, long insuranceCompanyId = -1, bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            items.AddRange(ServiceFactory.InsuranceCompanyServiceInstance.GetInsuranceCompanyAll(isParent).ToList().Select(o => new SelectListItem
            {
                Text = o.CompanyName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text", insuranceCompanyId);
        }

        /// <summary>
        /// 承保公司理赔员
        /// </summary>
        /// <param name="insuranceCompanyID">承保公司ID</param>
        /// <param name="addEmptySelect"></param>
        /// <returns></returns>
        public SelectList InsuranceCompanyUserConvertToSelectList(long insuranceCompanyID, bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            items.AddRange(ServiceFactory.SystemUserServiceInstance.GetSystemUserAllByInsuranceCompanyId(insuranceCompanyID).ToList().Select(o => new SelectListItem
            {
                Text = o.UserDisPlayName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }

        /// <summary>
        /// 工单状态
        /// </summary>
        /// <param name="excludeIds">需要排除的状态值</param>
        /// <param name="parentId">父级ID</param>
        /// <param name="addEmptySelect">是否需要默认项</param>
        /// <returns>主键ID：value 状态名称：text</returns>
        public SelectList InsuranceWarrantyRecordStatusConvertToSelectList(List<int> excludeCode, int parentId = 0, bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            items.AddRange(ServiceFactory.InsuranceWarrantyRecordStatusServiceInstance.GetChildrenStatus(excludeCode, parentId).ToList().Select(o => new SelectListItem
            {
                Text = o.StatusName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }

        /// <summary>
        /// 工单状态
        /// </summary>
        /// <param name="excludeCode">需要排除的状态值</param>
        /// <param name="parentCode">父级ID</param>
        /// <param name="addEmptySelect">是否需要默认项</param>
        /// <returns>状态码：value 状态名称：text</returns>
        public SelectList InsuranceWarrantyRecordStatusConvertToSelectList(byte parentCode, List<int> excludeCode, bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            items.AddRange(ServiceFactory.InsuranceWarrantyRecordStatusServiceInstance.GetChildrenStatus(parentCode, excludeCode).ToList().Select(o => new SelectListItem
            {
                Text = o.StatusName,
                Value = o.StatusCode.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }
        /// <summary>
        /// 根据数据中有的工单状态 进行展示 SelectList InsuranceWarrantyRecordConvertToSelectList(byte pageType, bool addEmptySelect = true)
        /// </summary>
        /// <param name="pageType">父级ID</param>
        /// <param name="addEmptySelect">是否需要默认值</param>
        /// <returns>状态码：value 状态名称：text</returns>
        public SelectList InsuranceWarrantyRecordConvertToSelectList(byte pageType, bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            items.AddRange(
                 ServiceFactory.InsuranceWarrantyRecordServiceInstance.LoadByInsuranceWarrantyRecordTaskType(pageType)
                     .GroupBy(m => m.Status).Select(n => new { n.Key }).AsEnumerable()
                     .Select(o => new SelectListItem
                     {
                         Text = ServiceFactory.InsuranceWarrantyRecordStatusServiceInstance.GetStatusName(o.Key),
                         Value = o.Key.ToString()
                     }).ToList());
            return new SelectList(items, "Value", "Text");
        }

        public SelectList RolesConvertToSelectList(bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            items.AddRange(ServiceFactory.SystemRoleServiceInstance.LoadAll().ToList().Select(o => new SelectListItem
            {
                Text = o.RoleName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }
        public SelectList PageActionConvertToSelectList(bool addEmptySelect = true)
        {
            List<SelectListItem> items = GetEmptySelect(addEmptySelect);
            items.AddRange(ServiceFactory.PageActionServiceInstance.LoadEntities(o => true).ToList().Select(o => new SelectListItem
            {
                Text = o.ActionName,
                Value = o.ID.ToString()
            }));
            return new SelectList(items, "Value", "Text");
        }

    }
}
